import { createRouter, createWebHistory } from 'vue-router'

import { useAppStore } from '@/stores/modules/app'

import routes from './routes'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

router.beforeEach(to => {
  // 不用登录的页面
  if (to.meta.ignoreAuth) {
    return true
  }

  const appStore = useAppStore()
  if (!appStore.isLogined && to.path !== '/sign-in') {
    const redirect = to.query.redirect || to.fullPath
    return `/sign-in?redirect=${redirect}`
  }
})

export default router
